package com.csdig.cms.web.front;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.csdig.cms.common.ConstantDefine;
import com.csdig.cms.service.FrontPageService;
import com.csdig.cms.utils.FrontUtils;
import com.csdig.cms.web.URLHelper;

@Controller
public class DynamicPageAct {

	@Autowired
	private FrontPageService pageService;

	/**
	 * @param request
	 * @param model
	 * @return
	 */
//	@RequestMapping(value = "/index.jhtml", method = RequestMethod.GET)
	public String index(HttpServletRequest request, ModelMap model) throws Exception {
		FrontUtils.frontGlobleData(model,"/index",ConstantDefine.FrontPage.DYNAMIC_EXTENTION);
		return ConstantDefine.FrontPage.TPL_BASE + "/" + ConstantDefine.FrontPage.TPL_SOLUTION + "/index/"
				+ ConstantDefine.FrontPage.INDEX + ConstantDefine.FrontPage.TPL_SUFFIX;
	}

	@RequestMapping(value = "/**/*.*", method = RequestMethod.GET)
	public String dynamic(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception {
		int pageNo = URLHelper.getPageNo(request);
		String[] params = URLHelper.getParams(request);
		// PageInfo info = URLHelper.getPageInfo(request);
		String[] paths = URLHelper.getPaths(request);
		int len = paths.length;
		FrontUtils.frontGlobleData(model,URLHelper.getURI(request),ConstantDefine.FrontPage.DYNAMIC_EXTENTION);
		if (len == 0) {
			return ConstantDefine.FrontPage.TPL_BASE + "/" + ConstantDefine.FrontPage.TPL_SOLUTION + "/index/"
					+ ConstantDefine.FrontPage.INDEX + ConstantDefine.FrontPage.TPL_SUFFIX;
		}else if (len == 1) {
			// 单页
			return pageService.channel(paths[0], pageNo, params, model);
			// return channel(paths[0], pageNo, params, info, request, response, model);
		} else {
			if (paths[len-1].equals(ConstantDefine.FrontPage.INDEX)) {
				// 栏目页
				return pageService.channel(paths[len-2], pageNo, params, model);
				// return channel(paths[0], pageNo, params, info, request, response, model);
			} else {
				// 内容页
				return pageService.content(paths[len-1], pageNo, params, model);
			}
		} 
	}

}
